package goodbaby.dklt.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.classic.core.activity.BaseActivity;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;
import com.tgd.gbb.uikit.ui.refresh.PullToRefreshScrollView;
import com.tgd.gbb.uikit.ui.refresh.base.PullToRefreshBase;
import com.tgd.gbb.uikit.ui.widget.MyListView;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import goodbaby.dklt.R;
import goodbaby.dklt.adapter.LeaveHistoryAdapter;
import goodbaby.dklt.bean.LeaveBean;

/**
 * Created by Jayden on 2016/5/4 11:26.
 *
 * @version V1.0
 * @Title: LeaveHistoryActivity
 * @Description: 请假历史
 */
public class LeaveHistoryActivity extends BaseActivity {
    public static final String TAG = "MessageActivity";

    private LeaveHistoryAdapter adapter;
    private ImageView ivBack;
    private TextView tvTitle;

    private MyListView listView;

    private PullToRefreshScrollView mPullRefreshScrollView;

    private LeaveHistoryAdapter mAdapter;

    private List<LeaveBean> mDatas = new ArrayList<LeaveBean>();

    private int pageNum = 1;

    public int getLayoutResId() {
        return R.layout.activity_leave_history;
    }

    public void initData() {
        super.initData();
    }

    public void initView() {
        super.initView();
        tvTitle = (TextView) findViewById(R.id.tv_common_title);
        tvTitle.setText("请假记录");
        ivBack = (ImageView) findViewById(R.id.iv_common_back);
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        listView = (MyListView) findViewById(R.id.lv_leave_history);
        mPullRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pull_scrollview);

        //上拉、下拉设定
        mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);

        mPullRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {

                if (mPullRefreshScrollView.isHeaderShown()) {
                    //下拉刷新
                    toRefresh();
                } else if (mPullRefreshScrollView.isFooterShown()) {
                    //上拉加载
                    toLoadMore();
                }

            }
        });

        mAdapter = new LeaveHistoryAdapter(this, mDatas);

        listView.setAdapter(mAdapter);

        listView.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadData();
            }
        }, 600);
    }

    private void loadData() {
        mPullRefreshScrollView.setRefreshing();
    }

    private void toRefresh() {
        pageNum = 1;
        ActionHelp.gbbLeaveDetail(this, pageNum, new ObjectCallback<List<LeaveBean>>() {

            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(LeaveHistoryActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                }
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                Log.i(TAG, "请求错误" + code + ";" + errorMessage);
                ToastUtil.showToast(LeaveHistoryActivity.this, "失败");
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(LeaveHistoryActivity.this, "失败");
            }

            public void onSuccess(List<LeaveBean> paramAnonymousList) {
                pageNum++;
                mPullRefreshScrollView.onRefreshComplete();
                mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
                mDatas.clear();
                mDatas.addAll(paramAnonymousList);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public Type getType() {
                return new TypeToken<List<LeaveBean>>() {
                }.getType();
            }
        });

    }

    private void toLoadMore() {
        ActionHelp.gbbLeaveDetail(this, pageNum, new ObjectCallback<List<LeaveBean>>() {

            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(LeaveHistoryActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                }
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                Log.i(TAG, "请求错误" + code + ";" + errorMessage);
                ToastUtil.showToast(LeaveHistoryActivity.this, "失败");
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(LeaveHistoryActivity.this, "失败");
            }

            public void onSuccess(List<LeaveBean> paramAnonymousList) {
                pageNum++;
                mDatas.addAll(paramAnonymousList);
                mAdapter.notifyDataSetChanged();
                mPullRefreshScrollView.onRefreshComplete();
                if (paramAnonymousList != null && paramAnonymousList.size() < 10) {
                    mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                } else {
                    mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
                }
            }

            @Override
            public Type getType() {
                return new TypeToken<List<LeaveBean>>() {
                }.getType();
            }
        });
    }


    public void viewClick(View paramView) {
        super.viewClick(paramView);
        switch (paramView.getId()) {
            default:
                break;
            case R.id.iv_common_back:
                finish();
                break;
        }
    }
}
